package com.xc.file.web.rest;

import com.xc.core.annotation.Authority;
import com.xc.core.bean.QueryBean;
import com.xc.core.dto.FileDto;
import com.xc.core.model.TokenModel;
import com.xc.file.bean.FileBean;
import com.xc.file.bean.UpdateFileBean;
import com.xc.file.dto.CdnDto;
import com.xc.file.entity.FileEntity;
import com.xc.file.service.FileService;
import com.xc.tool.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>文件管理rest接口</p>
 *
 * @author xc
 * @version v1.0.0
 */
@Api(tags = "文件管理rest接口")
@RestController
public class FileRest {

    @Autowired
    private FileService fileService;


    @ApiOperation(value = "删除文件", notes = "删除当前用户的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "fileId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @DeleteMapping(value = "/current_user/file/{fileId}")
    @Authority
    public FileDto deleteCurrentUserFile(TokenModel tokenModel, @PathVariable String fileId) {
        FileEntity fileEntity = fileService.deleteUserFile(tokenModel.getUserIds(), fileId);
        return ObjectUtils.convert(new FileDto(), fileEntity);
    }

    @ApiOperation(value = "修改文件", notes = "修改当前用户的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "fileId", paramType = "path", required = true),
            @ApiImplicitParam(value = "文件名称", name = "name", paramType = "body", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PutMapping(value = "/current_user/file/{fileId}")
    @Authority
    public FileDto updateCurrentUserFile(TokenModel tokenModel, @PathVariable("fileId") String fileId, @RequestBody UpdateFileBean updateFileBean) {
        return fileService.updateUserFile(tokenModel.getUserIds(), fileId, updateFileBean);
    }

    @ApiOperation(value = "获取文件集合", notes = "获取当前用户的文件集合")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @GetMapping(value = "/current_user/files")
    @Authority
    public List<FileDto> getCurrentUserFiles(TokenModel tokenModel, @ModelAttribute QueryBean queryBean, @ModelAttribute FileBean fileBean) {
        return fileService.getUserFileList(tokenModel.getUserId(), tokenModel.getUserIds(), queryBean, ObjectUtils.convert(new FileEntity(), fileBean));
    }

    @ApiOperation(value = "生成cdn地址", notes = "创建当前用户的cdn地址")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PostMapping(value = "/current_user/file/{fileId}/cdn_url")
    @Authority
    public CdnDto createCdnUrl(TokenModel tokenModel, @PathVariable String fileId) {
        return fileService.createCdnUrl(tokenModel.getUserId(), fileId);
    }
}
